//由于未知的保存时电脑出现bug导致之前的文件不能使用，因此改名为YunYingStore1
import { observable, action, runInAction } from 'mobx';
import { message } from 'antd'
import * as api from "../../service/api/YunYingApi";
import { formatDate } from '../../utils/utils'
let connected = false//websocket连接过了
class YunYingStore {
    @observable visible = true//云鹰的video播放第一个么
    @observable cameraList = []
    @observable warnShow = false
    @observable beginTime = undefined
    @observable endTime = undefined
    @observable imageBase64 = undefined
    @observable attrs = {}
    @observable YunYingModalShow = false
    // @observable YunYingModalShow = true
    @observable WarnMessage = {}
    @observable WarnEd = false//报过警报了
    @observable WhetherWarning = false
    @observable SearchBtnDisabled = true
    @observable SearchResultShow = false
    @observable Ws = undefined//存放websocket
    @observable WordArr = ["搜索第一步选择日期", '搜索第二步选择人体', '搜索第三步选择属性', '点击搜索']
    @observable NowWhichStep = ["搜索第一步选择日期"]//现在是第几步
    @observable UploadShow = false
    @observable ChooseAttrShow = false
    @observable SearchShow = false

    @action ChangeSearchShow = (condition) => {
        this.SearchShow = condition
    }
    @action ChangeChooseAttrShow = (condition) => {
        this.ChooseAttrShow = condition
    }
    @action ChangeUploadShow = (condition) => {
        this.UploadShow = condition
    }
    @action AddNowWhichStep = (StepName) => {
        this.NowWhichStep.push(StepName)
    }
    @action ChangeWhetherWarning = (condition) => {
        let that = this
        this.WhetherWarning = condition
        if (condition) {
            WebSocketTest()
            function WebSocketTest() {
                if (!connected) {
                    if ("WebSocket" in window) {
                        // 打开一个 web socket
                        var ws = new WebSocket("ws://192.168.1.102:32404/yunli-ai/alarm");
                        connected = true
                        that.Ws = ws
                        ws.onopen = function () {
                            // Web Socket 已连接上，使用 send() 方法发送数据
                            console.log("数据发送中...")
                            ws.send("发送数据");
                        };
                        ws.onmessage = async function (evt) {
                            var received_msg = evt.data;
                            if (!that.WarnEd) {
                                if (received_msg != 'ok' && received_msg != 'open connect success') {
                                    that.changeWarnEd(true)
                                    received_msg = JSON.parse(received_msg)
                                    that.changeWarnShow(true)
                                    that.changeWarnModaShow(true)
                                    await that.StorageWarnMessage(received_msg, '111')
                                }
                            }
                        };
                        ws.onclose = function () {
                            // 关闭 websocket
                            console.log('websocket连接已关闭...')
                        };
                    } else {
                        // 浏览器不支持 WebSocket
                        alert("您的浏览器不支持 WebSocket!");
                    }
                } else {
                    console.log(that.Ws)
                    that.Ws.onopen()
                }
            }
        } else {
            this.Ws.onclose()
        }
    }
    @action changeSearchResultShow = (condition) => {
        this.SearchResultShow = condition
    }

    @action changeWarnEd = (condition) => {
        this.WarnEd = condition
    }
    @action StorageWarnMessage = async (WarnMessage, token) => {
        const res = await api.searchImgSrc(WarnMessage.src, token)
        const res1 = await api.searchImgSrc(WarnMessage.cropSrc, token)
        // console.log(res)
        if (res.code === 200) {
            WarnMessage.src = res.data
        }
        if (res1.code === 200) {
            WarnMessage.cropSrc = res1.data
        }
        WarnMessage.ts = formatDate(WarnMessage.ts)

        WarnMessage.id = WarnMessage.results[0].id
        WarnMessage.score = WarnMessage.results[0].score
        runInAction(() => {
            this.WarnMessage = WarnMessage
            console.log('store中改变了WarnMessage', WarnMessage)
        })
    }
    @action changeWarnModaShow = (condition) => {
        this.YunYingModalShow = condition
    }
    @action changeWarnShow = (condition) => {
        this.warnShow = condition
    }
    @action async search(token, beginTime, endTime, imgSrc, attrs, threshold, top) {
        const res = await api.search({
            attrs,
            "beginTime": beginTime,
            "endTime": endTime,
            "imgSrc": imgSrc,
            "threshold": threshold,
            "top": top
        }, token)
        console.log(res)
        if (res.code === 1001 && res.msg === 'At least one imgSrc and attrs cannot be empty') {
            message.info('请选择图片，并设置属性')
        } else {
            runInAction(() => {
                this.SearchResultShow = true
            })

        }
    }
    @action StorageAttrs = (attrs) => {
        this.attrs[attrs] = 0.8
        console.log('json为', this.attrs)
    }
    @action storageImageBase64 = (imageBase64) => {
        let imgUrl = imageBase64
        if (typeof imgUrl == "string" && imgUrl.length > 200) {
            imgUrl = imgUrl.replace("data:image/png;base64,", "base64://");
            imgUrl = imgUrl.replace("data:image/jpg;base64,", "base64://");
            imgUrl = imgUrl.replace("data:image/jpeg;base64,", "base64://");
        }
        this.imageBase64 = imgUrl
    }
    @action changeTime = (beginTime, endTime) => {
        this.beginTime = beginTime
        this.endTime = endTime
    }
    @action changeVisible = (condition) => {
        this.visible = condition
    }
    @action getDeviceList = async () => {
        let result = []
        const res = await api.getDeviceList()
        console.log(res)
        runInAction(() => {
            if (res.code == 200) {
                console.log('获取摄像头列表成功')
                let arr = []
                for (let [index, item] of res.data.results.entries()) {
                    arr.push(item.name)
                }
                this.cameraList = arr
                result = arr
            } else {
                console.log('获取摄像头列表失败')
            }
        })
        return result
    }
} export default new YunYingStore();